package com.chinaisafe.common.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;

/**
 * SpringMvcResponseJacksonConfig
 * Description：SpringMvc全局Json序列化返回配置（用于向前端响应数据）
 *
 * @author JiangZhaoYong
 * @date 2024/7/10 17:37
 */
@Configuration
public class SpringMvcResponseJacksonConfig {

    @Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
    private String dateTimePattern;

    private final String datePattern = "yyyy-MM-dd";

    private final String timePattern = "HH:mm:ss";

    @Bean
    public BeanPostProcessor objectMapperBeanPostProcessor() {
        return new BeanPostProcessor() {
            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                if (!(bean instanceof ObjectMapper objectMapper)) {
                    return bean;
                }
                // 全局配置序列化返回 JSON 处理
                // 日期时间
                SimpleModule dateTimeSimpleModule = new SimpleModule();
                dateTimeSimpleModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimePattern)));
                dateTimeSimpleModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(dateTimePattern)));

                // 日期
                SimpleModule dateSimpleModule = new SimpleModule();
                dateSimpleModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(datePattern)));
                dateSimpleModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(datePattern)));

                // 时间
                SimpleModule timeSimpleModule = new SimpleModule();
                timeSimpleModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(timePattern)));
                timeSimpleModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(timePattern)));

                SimpleModule module = new SimpleModule();
                module.addSerializer(Long.TYPE, ToStringSerializer.instance);
                module.addSerializer(Long.class, ToStringSerializer.instance);

                objectMapper.registerModules(dateTimeSimpleModule, dateSimpleModule, timeSimpleModule, module);
                objectMapper.setTimeZone(TimeZone.getDefault());



                return bean;
            }
        };
    }
}
